package zhoug.common.imageloader.wrap;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.RequestBuilder;
import zhoug.common.imageloader.CircleImageTarget;
import zhoug.common.imageloader.RoundedCornerImageTarget;

import java.io.File;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;

/**
 * @Author 35574
 * @Date 2021/9/7
 * @Description
 */
public class RequestBuilderWrap<TranscodeType> {
    RequestBuilder<TranscodeType> mRequestBuilder;
    private RequestOptionBuild mRequestOptionBuild;

    private RequestBuilderWrap(RequestBuilder<TranscodeType> requestBuilder) {
        this.mRequestBuilder = requestBuilder;
    }

    public static <TranscodeType> RequestBuilderWrap<TranscodeType> create(@NonNull RequestBuilder<TranscodeType> requestBuilder) {
        return new RequestBuilderWrap<>(requestBuilder);
    }


    public RequestBuilderWrap<TranscodeType> load(@Nullable Bitmap bitmap) {
        mRequestBuilder = mRequestBuilder.load(bitmap);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@Nullable Drawable drawable) {
        mRequestBuilder = mRequestBuilder.load(drawable);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@Nullable String string) {
        mRequestBuilder = mRequestBuilder.load(string);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@Nullable Uri uri) {
        mRequestBuilder = mRequestBuilder.load(uri);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@Nullable File file) {
        mRequestBuilder = mRequestBuilder.load(file);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@RawRes @DrawableRes @Nullable Integer resourceId) {
        mRequestBuilder = mRequestBuilder.load(resourceId);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@Nullable byte[] model) {
        mRequestBuilder = mRequestBuilder.load(model);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> load(@Nullable Object model) {
        mRequestBuilder = mRequestBuilder.load(model);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> thumbnail(RequestBuilder<TranscodeType> requestBuilder){
        mRequestBuilder=mRequestBuilder.thumbnail(requestBuilder);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> thumbnail(RequestBuilder<TranscodeType>... requestBuilder){
        mRequestBuilder=mRequestBuilder.thumbnail(requestBuilder);
        return this;
    }



    public void into(ImageView imageView) {
        boolean isCircle = false;
        int roundedCorners = 0;
        if (mRequestOptionBuild != null) {
            isCircle = mRequestOptionBuild.isCircle();
            roundedCorners=mRequestOptionBuild.getRoundedCorners();
            mRequestBuilder = mRequestBuilder.apply(mRequestOptionBuild.build());
        }
        if(isCircle){
            mRequestBuilder.into(new CircleImageTarget<>(imageView));
        }else if(roundedCorners>0){
            mRequestBuilder.into(new RoundedCornerImageTarget<>(imageView,roundedCorners));
        }else{
            mRequestBuilder.into(imageView);
        }
    }

    private RequestOptionBuild _getRequestOptionBuild() {
        if (mRequestOptionBuild == null) {
            mRequestOptionBuild = new RequestOptionBuild();
        }
        return mRequestOptionBuild;
    }

    public RequestBuilderWrap<TranscodeType> transformCircle(boolean isCircle) {
        _getRequestOptionBuild().setCircle(isCircle);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> transformGrayscale(boolean isGrayscale) {
        _getRequestOptionBuild().setGrayscale(isGrayscale);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> placeholder(@DrawableRes int resourceId) {
        _getRequestOptionBuild().placeholder(resourceId);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> placeholder(Drawable placeholder) {
        _getRequestOptionBuild().placeholder(placeholder);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> error(@DrawableRes int resourceId) {
        _getRequestOptionBuild().error(resourceId);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> error(Drawable error) {
        _getRequestOptionBuild().error(error);
        return this;
    }

    public RequestBuilderWrap<TranscodeType> setRoundedCorners(int roundedCorners) {
        _getRequestOptionBuild().setRoundedCorners(roundedCorners);
        return this;
    }


}
